/*
 * Copyright (C) 2011 SHARP Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jp.co.sharp.android.yourlocation.data;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SettingDBHelper extends SQLiteOpenHelper {

	private static final String DB_NAME = "Setting.db";
	private static final int DB_VERSION = 1;

	public static final int INSERT_RESULT_SUCCESS = 0;
	public static final int INSERT_RESULT_DUPLICATE = 1;
	public static final int INSERT_RESULT_ROW_LIMIT_EXCEED = 2;

	public static final int DELETE_RESULT_SUCCESS = 0;
	public static final int DELETE_RESULT_NOT_FOUND = 1;

	public static final int SETTING_ID_DISCLAIMER = 1;
	public static final int SETTING_VALUE_DISCLAIMER_ON = 1;
	public static final int SETTING_VALUE_DISCLAIMER_OFF = 0;

	public static final int SETTING_ID_POSITIONING_TIMEOUT = 2;
	public static final int SETTING_VALUE_POSITIONING_TIMEOUT_DEFAULT = 60000;

	public static final int SETTING_ID_POSITIONING_MAX_RETRY = 3;
	public static final int SETTING_VALUE_POSITIONING_MAX_RETRY_DEFAULT = 2;

	private static final String TABLE_SETTING = "setting";
	private static final String COLUMN_SETTING_PK = "_id";
	private static final String COLUMN_SETTING_ID = "setting_id";
	private static final String COLUMN_SETTING_VALUE = "setting_value";

	private static final String SQL_CREATE_TABLE = "CREATE TABLE setting ("
			+ "_id integer primary key autoincrement," + "setting_id integer,"
			+ "setting_value integer" + ");";

	public SettingDBHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}

	public void onCreate(SQLiteDatabase db) {
		try {
			db.execSQL(SQL_CREATE_TABLE);
			insertSettingRecord(db, SETTING_ID_DISCLAIMER,
					SETTING_VALUE_DISCLAIMER_ON);
			insertSettingRecord(db, SETTING_ID_POSITIONING_TIMEOUT,
					SETTING_VALUE_POSITIONING_TIMEOUT_DEFAULT);
			insertSettingRecord(db, SETTING_ID_POSITIONING_MAX_RETRY,
					SETTING_VALUE_POSITIONING_MAX_RETRY_DEFAULT);
		} catch (SQLException e) {
			Log.e("SettingDBHelper", e.getMessage());
		}
	}

	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// do nothing
	}

	private int insertSettingRecord(SQLiteDatabase db, int setting_id,
			int setting_value) {

		ContentValues values = new ContentValues();
		values.put(COLUMN_SETTING_ID, setting_id);
		values.put(COLUMN_SETTING_VALUE, setting_value);

		db.insert(TABLE_SETTING, null, values);

		return INSERT_RESULT_SUCCESS;
	}

	public int selectSettingRecord(int setting_id) {
		final SQLiteDatabase db = this.getReadableDatabase();
		int retval = 0;

		Cursor cursor = db.query(TABLE_SETTING, new String[] {
				COLUMN_SETTING_PK, COLUMN_SETTING_ID, COLUMN_SETTING_VALUE },
				COLUMN_SETTING_ID + "=" + setting_id, null, null, null, null,
				null);
		if (cursor.getCount() > 0 && cursor.moveToFirst()) {
			retval = cursor.getInt(cursor.getColumnIndex(COLUMN_SETTING_VALUE));
		}
		cursor.close();

		db.close();

		return retval;
	}

	public int updateSettingRecord(int setting_id, int setting_value) {
		final SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(COLUMN_SETTING_VALUE, setting_value);

		db.update(TABLE_SETTING, values, COLUMN_SETTING_ID + "=" + setting_id,
				null);

		db.close();

		return DELETE_RESULT_SUCCESS;
	}
}
